import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.List;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Window.java
 *
 * Created on Jan 4, 2012, 6:52:45 PM
 */
/**
 *
 * @author Figueroa
 */
public class Editor extends javax.swing.JApplet implements MouseListener, MouseMotionListener {

    /** Initializes the applet Window */
    @Override
    public void init() {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Window.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the applet */
    
             this.setSize(870,510);
             resize(size().width < 200 ? 200 : size().width,
	     size().height < 200 ? 200 : size().height);
             
     
         

        initComponents();
        bg.add(jToggleButton1);
        bg.add(eliminarbtn);
        buttonGroup1.add(negrasrb);
        buttonGroup1.add(blancasrb);
        blancasrb.setSelected(true);
        PanelBtn.setLayout(new GridLayout(2,2));
        PanelTools.setLayout(new GridLayout(2,2));
        
        
        
     Graphics g=getGraphics();
        interfaz(g);
     
    }
    
    private Board tablero;
  
    public void interfaz(Graphics g){
      Backbtn.setVisible(false);
      accept_solution.setVisible(false);
      toolbox.setLayout(new GridLayout(6,2));
      toolbox.setPreferredSize(new java.awt.Dimension(300,200));
      toolbox.setMaximumSize(toolbox.getPreferredSize());
      tablero = new Board();
      tablero.setSize(450, 450);
      tablero.addMouseListener(this);
      tablero.addMouseMotionListener(this);
      tablero.paint(g);
    
   
    dibujar_toolbox();
    toolbox.revalidate();
      
     PanelBoard.add( tablero, null);
     PanelBoard.setSize(tablero.getSize());
         
    ConectorBD conex = new ConectorBD();
     conex.conectar();
     conex.consulta();
     List<String> niveles = conex.get_niveles();
     nivelcmb.removeAllItems();
     for(int i=0;i<niveles.size();i++){
     nivelcmb.addItem(niveles.get(i));
     }
   

    }
    
    
   public void dibujar_toolbox(){
   
   String pawnW  = "\u2659";
   String kingW  = "\u2654";
   String queenW = "\u2655";
   String rookW  = "\u2656";
   String bishopW = "\u2657";
   String knightW = "\u2658";
 
   //Black set
   String pawnB  = "\u265F";
   String kingB  = "\u265A";
   String queenB = "\u265B";
   String rookB  = "\u265C";
   String bishopB = "\u265D";
   String knightB = "\u265E";
   
   String [] piece = new String[] {pawnW,kingW,queenW,rookW,bishopW,knightW,pawnB,
                                  kingB,queenB,rookB,bishopB,knightB};
   
  
   
   
    for (int i=0; i<piece.length; i++){
     int fontsize = 22 ;
     JToggleButton nuevo = new JToggleButton();
     nuevo.setFont(new Font("TimesRoman", Font.PLAIN,fontsize));
     nuevo.setText(piece[i]);
     nuevo.setToolTipText("Seleccionar "+tablero.get_piece_name(piece[i]));
    nuevo.addActionListener(new ActionListener( ) {
      public void actionPerformed(ActionEvent ev) {
          JToggleButton selected = (JToggleButton)ev.getSource();
          SelectedPiece = selected.getText();
        
      }});

     bg.add(nuevo);
     
     
     toolbox.add(nuevo,null);
     
       
       }
   
   }




   

    @Override
    public void mouseClicked(MouseEvent e) {
       if(e.getSource() == tablero){
       Graphics g=this.getGraphics();
           if(SelectedPiece !=null){
              int x = e.getX();
              int y = e.getY();
              int i=tablero.posinv(x);
              int j=tablero.posinv(y);
             
              if(!SelectedPiece.equals("Clean")){
                    tablero.piece[j*8+i]= tablero.get_piece(SelectedPiece);
                 }
              else{
                 tablero.piece[j*8+i] =' ';
              }
              
              tablero.update(g);
             
           }
       }
    
    
    }

    @Override
    public void mousePressed(MouseEvent e) {
  
        if( e.getSource() == tablero){
         Graphics g=this.getGraphics();
         int x = e.getX();
         int y = e.getY();
         int i=tablero.posinv(x);
         int j=tablero.posinv(y);
         // System.out.println("I"+i+"J"+j);
      if (i>=8 || j>=8) {
         return;
      }
      //  do nothing if square is empty
      if (tablero.piece[j*8+i]==' ') {
         return;
      }
      //  do nothing if square is not one of the
      //  pieces of the color whose turn it is
    /*  if (tablero.whiteTurn!=tablero.isWhite(tablero.piece[j*8+i])) {
         return;
      }*/
      
      // start dragging a piece      
      tablero.dragging=true;
      tablero.temp=false;
      tablero.dragCol=i;
      tablero.dragRow=j;
      tablero.dragPiece=tablero.piece[j*8+i];
      // highlight the selected square in red
      tablero.drawSquare(g,i,j,true);
      return;
    }
    else if(e.getSource() == solutionBoard){
             Graphics g=solutionBoard.getGraphics();
         int x = e.getX();
         int y = e.getY();
         int i=solutionBoard.posinv(x);
         int j=solutionBoard.posinv(y);
        // System.out.println("I"+i+"J"+j);
      if (i>=8 || j>=8) {
         return;
      }
      //  do nothing if square is empty
      if (solutionBoard.piece[j*8+i]==' ') {
         return;
      }
      //  do nothing if square is not one of the
      //  pieces of the color whose turn it is
     if (solutionBoard.whiteTurn!=solutionBoard.isWhite(solutionBoard.piece[j*8+i])) {
         return;
      }
      
      // start dragging a piece      
      solutionBoard.dragging=true;
      solutionBoard.temp=false;
      solutionBoard.dragCol=i;
      solutionBoard.dragRow=j;
      solutionBoard.dragPiece=solutionBoard.piece[j*8+i];
      // highlight the selected square in red
      solutionBoard.drawSquare(g,i,j,true);
      return;
        }
    
    }

    @Override
    public void mouseReleased(MouseEvent e) {
         if( e.getSource() == tablero){
     Graphics g=this.getGraphics();
     int x = e.getX();
     int y = e.getY(); 
     int i=tablero.posinv(x);
      int j=tablero.posinv(y);
      if (tablero.dragging) {
         if (tablero.temp && i>=0 && j<8
                  && !(i==tablero.dragCol && j==tablero.dragRow)) {
            // make the temporary move permanent
            tablero.drawSquare(g,tablero.tempCol,tablero.tempRow,false);
            // remove the piece from its old position
            tablero.piece[tablero.dragRow*8+tablero.dragCol]=' ';
             // place piece in its new position
            tablero.piece[j*8+i]=tablero.dragPiece;
            tablero.drawSquare(g,i,j,false);
            // change the turn
        //    tablero.whiteTurn=!tablero.whiteTurn;
         }
         // remove highlighting on old position
         tablero.drawSquare(g,tablero.dragCol,tablero.dragRow,false);
         tablero.temp=false;
         tablero.dragging=false;
      }
      return;
    }
         else   if( e.getSource() == solutionBoard){
     Graphics g=solutionBoard.getGraphics();
     int x = e.getX();
     int y = e.getY(); 
     int i=solutionBoard.posinv(x);
      int j=solutionBoard.posinv(y);
      if (solutionBoard.dragging) {
         if (solutionBoard.temp && i>=0 && j<8
                  && !(i==solutionBoard.dragCol && j==solutionBoard.dragRow)) {
            // make the temporary move permanent
            solutionBoard.drawSquare(g,solutionBoard.tempCol,solutionBoard.tempRow,false);
            // remove the piece from its old position
            solutionBoard.piece[solutionBoard.dragRow*8+solutionBoard.dragCol]=' ';
             // place piece in its new position
            solutionBoard.piece[j*8+i]=solutionBoard.dragPiece;
            solutionBoard.drawSquare(g,i,j,false);
            if(!solutionBoard.whiteTurn){
              Registry +=","+solutionBoard.translate_move(solutionBoard.dragRow, solutionBoard.dragCol, j, i, solutionBoard.dragPiece)+"\n";
            }
            else{
                Registry += ""+solutionBoard.translate_move(solutionBoard.dragRow, solutionBoard.dragCol, j, i, solutionBoard.dragPiece);
            }
           solutiontxt.setText(Registry);
            // change the turn
          solutionBoard.whiteTurn=!solutionBoard.whiteTurn;
          
         }
         // remove highlighting on old position
         solutionBoard.drawSquare(g,solutionBoard.dragCol,solutionBoard.dragRow,false);
         solutionBoard.temp=false;
         solutionBoard.dragging=false;
      }
      return;
    }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
     //
    }

    @Override
    public void mouseExited(MouseEvent e) {
        //Do nothing
    }

    @Override
    public void mouseDragged(MouseEvent e) {
         if( e.getSource() == tablero){
   //  System.out.print("Mouse dragged");
        Graphics g=this.getGraphics();
       int x = e.getX();
       int y = e.getY();
       int i=tablero.posinv(x);
       int j=tablero.posinv(y);
       if (tablero.dragging) {
         // has nothing changed from last time?
         if (tablero.temp && i==tablero.tempCol && j==tablero.tempRow) {
            return ;
         }
         if (tablero.temp) {
            // clear the previous temporary move
            tablero.drawSquare(g,tablero.tempCol,tablero.tempRow,false);
            tablero.temp=false;
         }
         // check for legal move 
         if (tablero.legalMove(i,j)) {
            // update new position to show the piece
            tablero.temp=true;
            tablero.tempCol=i;
            tablero.tempRow=j;
            tablero.drawSquare(g,i,j,true);
         }
      }
      return ;
    }
         else     if( e.getSource() == solutionBoard){
   //  System.out.print("Mouse dragged");
        Graphics g=solutionBoard.getGraphics();
       int x = e.getX();
       int y = e.getY();
       int i=solutionBoard.posinv(x);
       int j=solutionBoard.posinv(y);
       if (solutionBoard.dragging) {
         // has nothing changed from last time?
         if (solutionBoard.temp && i==solutionBoard.tempCol && j==solutionBoard.tempRow) {
            return ;
         }
         if (solutionBoard.temp) {
            // clear the previous temporary move
            solutionBoard.drawSquare(g,solutionBoard.tempCol,solutionBoard.tempRow,false);
            solutionBoard.temp=false;
         }
         // check for legal move 
         if (solutionBoard.legalMove(i,j)) {
            // update new position to show the piece
            solutionBoard.temp=true;
            solutionBoard.tempCol=i;
            solutionBoard.tempRow=j;
            if(!solutionBoard.promotion){
            
            solutionBoard.drawSquare(g,i,j,true);
            }
            else{
                
             if(!solutionBoard.whiteTurn){
              Registry +=""+solutionBoard.translate_move(solutionBoard.dragRow, solutionBoard.dragCol, j, i, solutionBoard.dragPiece);
                }
            else{
                Registry += ","+solutionBoard.translate_move(solutionBoard.dragRow, solutionBoard.dragCol, j, i, solutionBoard.dragPiece)+"\n";
            }
                solutiontxt.setText(Registry);
                solutionBoard.promotion=false;
                solutionBoard.dragging = false;
                
                
                
                solutionBoard.update(g);
              }
         
         }
      }
      return ;
    }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
       // throw new UnsupportedOperationException("Not supported yet.");
    }
     private void RepaintPanelBoard(){
            PanelBoard.setBackground(new java.awt.Color(116, 165, 182));
        PanelBoard.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        PanelBoard.setName("PanelBoard"); // NOI18N
        PanelBoard.setPreferredSize(new java.awt.Dimension(452, 452));

        jLabel2.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel2.setText("8");

        jLabel3.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel3.setText("7");

        jLabel4.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel4.setText("6");

        jLabel5.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel5.setText("5");

        jLabel6.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel6.setText("4");

        jLabel7.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel7.setText("3");

        jLabel8.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel8.setText("2");

        jLabel9.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel9.setText("1");

        jLabel10.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel10.setText("A");

        jLabel11.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel11.setText("B");

        jLabel12.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel12.setText("C");

        jLabel13.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel13.setText("D");

        jLabel14.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel14.setText("E");

        jLabel15.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel15.setText("F");

        jLabel16.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel16.setText("G");

        jLabel17.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel17.setText("H");

        javax.swing.GroupLayout PanelBoardLayout = new javax.swing.GroupLayout(PanelBoard);
        PanelBoard.setLayout(PanelBoardLayout);
        PanelBoardLayout.setHorizontalGroup(
            PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelBoardLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelBoardLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel10)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel11)
                        .addGap(45, 45, 45)
                        .addComponent(jLabel12)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel13)
                        .addGap(40, 40, 40)
                        .addComponent(jLabel14)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel15)
                        .addGap(50, 50, 50)
                        .addComponent(jLabel16)
                        .addGap(52, 52, 52)
                        .addComponent(jLabel17)
                        .addContainerGap())
                    .addGroup(PanelBoardLayout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(458, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PanelBoardLayout.createSequentialGroup()
                        .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );
        PanelBoardLayout.setVerticalGroup(
            PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelBoardLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(30, 30, 30)
                .addComponent(jLabel3)
                .addGap(36, 36, 36)
                .addComponent(jLabel4)
                .addGap(36, 36, 36)
                .addComponent(jLabel5)
                .addGap(35, 35, 35)
                .addComponent(jLabel6)
                .addGap(41, 41, 41)
                .addComponent(jLabel7)
                .addGap(37, 37, 37)
                .addComponent(jLabel8)
                .addGap(44, 44, 44)
                .addComponent(jLabel9)
                .addGap(25, 25, 25)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12)
                    .addComponent(jLabel13)
                    .addComponent(jLabel14)
                    .addComponent(jLabel15)
                    .addComponent(jLabel16)
                    .addComponent(jLabel17))
                .addGap(17, 17, 17))
        );
     }


    private void load_playboard() {
       turn = false;
       if(blancasrb.isSelected())
       turn=true;
       PanelBoard.remove(tablero);
       RepaintPanelBoard();
       solutionBoard = new PlayBoard();
       solutionBoard.whiteTurn = turn;
       solutionBoard.piece = BoardStatus.clone();
       solutionBoard.setSize(450,450);
       solutionBoard.paint(this.getGraphics());
       solutionBoard.addMouseListener(this);
       solutionBoard.addMouseMotionListener(this);
       PanelBoard.add(solutionBoard,null);
      
       PanelBoard.revalidate();
    }
     private void load_editboard() {
       RepaintPanelBoard();
       tablero = new Board();
       tablero.piece = BoardStatus.clone();
       tablero.setSize(450,450);
       tablero.paint(this.getGraphics());
       tablero.addMouseListener(this);
       tablero.addMouseMotionListener(this);
       PanelBoard.add(tablero,null);
      
       PanelBoard.revalidate();
       PanelBoard.repaint();
    }
    
    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        buttonGroup3 = new javax.swing.ButtonGroup();
        buttonGroup4 = new javax.swing.ButtonGroup();
        buttonGroup5 = new javax.swing.ButtonGroup();
        buttonGroup6 = new javax.swing.ButtonGroup();
        PanelBoard = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        toolbox = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jLabel18 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel19 = new javax.swing.JLabel();
        nombretxt = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        nivelcmb = new javax.swing.JComboBox();
        jLabel21 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        descripciontxt = new javax.swing.JTextArea();
        jLabel22 = new javax.swing.JLabel();
        blancasrb = new javax.swing.JRadioButton();
        jLabel23 = new javax.swing.JLabel();
        negrasrb = new javax.swing.JRadioButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        solutiontxt = new javax.swing.JTextArea();
        PanelBtn = new javax.swing.JPanel();
        Backbtn = new javax.swing.JButton();
        accept_solution = new javax.swing.JButton();
        guardarbtn = new javax.swing.JButton();
        AddSolution = new javax.swing.JButton();
        PanelTools = new javax.swing.JPanel();
        jToggleButton1 = new javax.swing.JToggleButton();
        eliminarbtn = new javax.swing.JToggleButton();
        limpiarbtn = new javax.swing.JButton();
        Ordenarbtn = new javax.swing.JButton();

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 100, Short.MAX_VALUE)
        );

        PanelBoard.setBackground(new java.awt.Color(116, 165, 182));
        PanelBoard.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        PanelBoard.setName("PanelBoard"); // NOI18N
        PanelBoard.setPreferredSize(new java.awt.Dimension(452, 452));

        jLabel2.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel2.setText("8");

        jLabel3.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel3.setText("7");

        jLabel4.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel4.setText("6");

        jLabel5.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel5.setText("5");

        jLabel6.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel6.setText("4");

        jLabel7.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel7.setText("3");

        jLabel8.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel8.setText("2");

        jLabel9.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel9.setText("1");

        jLabel10.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel10.setText("A");

        jLabel11.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel11.setText("B");

        jLabel12.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel12.setText("C");

        jLabel13.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel13.setText("D");

        jLabel14.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel14.setText("E");

        jLabel15.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel15.setText("F");

        jLabel16.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel16.setText("G");

        jLabel17.setFont(new java.awt.Font("SansSerif", 1, 14));
        jLabel17.setText("H");

        javax.swing.GroupLayout PanelBoardLayout = new javax.swing.GroupLayout(PanelBoard);
        PanelBoard.setLayout(PanelBoardLayout);
        PanelBoardLayout.setHorizontalGroup(
            PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelBoardLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelBoardLayout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabel10)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel11)
                        .addGap(45, 45, 45)
                        .addComponent(jLabel12)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel13)
                        .addGap(40, 40, 40)
                        .addComponent(jLabel14)
                        .addGap(44, 44, 44)
                        .addComponent(jLabel15)
                        .addGap(50, 50, 50)
                        .addComponent(jLabel16)
                        .addGap(52, 52, 52)
                        .addComponent(jLabel17)
                        .addContainerGap())
                    .addGroup(PanelBoardLayout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(458, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PanelBoardLayout.createSequentialGroup()
                        .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 8, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                            .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
        );
        PanelBoardLayout.setVerticalGroup(
            PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelBoardLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addGap(30, 30, 30)
                .addComponent(jLabel3)
                .addGap(36, 36, 36)
                .addComponent(jLabel4)
                .addGap(36, 36, 36)
                .addComponent(jLabel5)
                .addGap(35, 35, 35)
                .addComponent(jLabel6)
                .addGap(41, 41, 41)
                .addComponent(jLabel7)
                .addGap(37, 37, 37)
                .addComponent(jLabel8)
                .addGap(44, 44, 44)
                .addComponent(jLabel9)
                .addGap(25, 25, 25)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(PanelBoardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12)
                    .addComponent(jLabel13)
                    .addComponent(jLabel14)
                    .addComponent(jLabel15)
                    .addComponent(jLabel16)
                    .addComponent(jLabel17))
                .addGap(17, 17, 17))
        );

        toolbox.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));
        toolbox.setLayout(new java.awt.GridLayout(1, 0));

        jPanel3.setLayout(new java.awt.GridLayout(1, 0));

        jPanel4.setLayout(new java.awt.GridLayout(1, 0));

        jPanel5.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jPanel5.setPreferredSize(new java.awt.Dimension(350, 500));

        jLabel18.setFont(new java.awt.Font("Copperplate Gothic Light", 1, 18));
        jLabel18.setText("Editor de Ejercicios");

        jLabel19.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel19.setText("Nombre:");

        nombretxt.setFont(new java.awt.Font("Tahoma", 0, 12));

        jLabel20.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel20.setText("Nivel:");

        nivelcmb.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel21.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel21.setText("Descripción:");

        descripciontxt.setColumns(20);
        descripciontxt.setRows(5);
        jScrollPane1.setViewportView(descripciontxt);

        jLabel22.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel22.setText("Juegan:");

        blancasrb.setText("Blancas");

        jLabel23.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel23.setText("Solución:");

        negrasrb.setText("Negras");

        solutiontxt.setColumns(20);
        solutiontxt.setEditable(false);
        solutiontxt.setRows(5);
        jScrollPane2.setViewportView(solutiontxt);

        Backbtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/back.png"))); // NOI18N
        Backbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BackbtnActionPerformed(evt);
            }
        });

        accept_solution.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/ok.png"))); // NOI18N
        accept_solution.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                accept_solutionActionPerformed(evt);
            }
        });

        guardarbtn.setFont(new java.awt.Font("Tahoma", 1, 14));
        guardarbtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Guardar.png"))); // NOI18N
        guardarbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                guardarbtnActionPerformed(evt);
            }
        });

        AddSolution.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/Add.png"))); // NOI18N
        AddSolution.setToolTipText("Agregar una solución alternativa");
        AddSolution.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddSolutionActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout PanelBtnLayout = new javax.swing.GroupLayout(PanelBtn);
        PanelBtn.setLayout(PanelBtnLayout);
        PanelBtnLayout.setHorizontalGroup(
            PanelBtnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelBtnLayout.createSequentialGroup()
                .addComponent(AddSolution, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(guardarbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Backbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(accept_solution, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        PanelBtnLayout.setVerticalGroup(
            PanelBtnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PanelBtnLayout.createSequentialGroup()
                .addGroup(PanelBtnLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(AddSolution, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                    .addComponent(guardarbtn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                    .addComponent(accept_solution, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                    .addComponent(Backbtn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel5Layout.createSequentialGroup()
                                .addComponent(jLabel19)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(nivelcmb, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(nombretxt, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)))
                            .addComponent(jLabel20)
                            .addComponent(jLabel18))
                        .addGap(36, 36, 36))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel21)
                        .addContainerGap(175, Short.MAX_VALUE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(17, Short.MAX_VALUE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel22)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(blancasrb)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(negrasrb)
                        .addContainerGap(73, Short.MAX_VALUE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(jLabel23)
                        .addContainerGap(194, Short.MAX_VALUE))
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)))
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addComponent(PanelBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(84, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGap(6, 6, 6)
                .addComponent(jLabel18)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel19)
                    .addComponent(nombretxt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20)
                    .addComponent(nivelcmb, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel21)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel22)
                    .addComponent(blancasrb)
                    .addComponent(negrasrb))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel23)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(PanelBtn, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(35, Short.MAX_VALUE))
        );

        jToggleButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/pointer.png"))); // NOI18N
        jToggleButton1.setToolTipText("Seleccionar puntero");
        jToggleButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jToggleButton1ActionPerformed(evt);
            }
        });

        eliminarbtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/xicon.png"))); // NOI18N
        eliminarbtn.setToolTipText("Eliminar fichas ");
        eliminarbtn.setPreferredSize(new java.awt.Dimension(73, 53));
        eliminarbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                eliminarbtnActionPerformed(evt);
            }
        });

        limpiarbtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/clean.png"))); // NOI18N
        limpiarbtn.setToolTipText("Limpiar el tablero");
        limpiarbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                limpiarbtnActionPerformed(evt);
            }
        });

        Ordenarbtn.setFont(new java.awt.Font("Copperplate Gothic Light", 0, 12)); // NOI18N
        Ordenarbtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Resources/chess-icon.png"))); // NOI18N
        Ordenarbtn.setToolTipText("Ordenar Tablero");
        Ordenarbtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OrdenarbtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout PanelToolsLayout = new javax.swing.GroupLayout(PanelTools);
        PanelTools.setLayout(PanelToolsLayout);
        PanelToolsLayout.setHorizontalGroup(
            PanelToolsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelToolsLayout.createSequentialGroup()
                .addGroup(PanelToolsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelToolsLayout.createSequentialGroup()
                        .addComponent(jToggleButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(eliminarbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(limpiarbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 53, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(PanelToolsLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(Ordenarbtn, 0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        PanelToolsLayout.setVerticalGroup(
            PanelToolsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(PanelToolsLayout.createSequentialGroup()
                .addGroup(PanelToolsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PanelToolsLayout.createSequentialGroup()
                        .addComponent(jToggleButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 62, Short.MAX_VALUE)
                        .addGap(4, 4, 4))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PanelToolsLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(limpiarbtn))
                    .addComponent(eliminarbtn, javax.swing.GroupLayout.Alignment.TRAILING, 0, 0, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Ordenarbtn, javax.swing.GroupLayout.PREFERRED_SIZE, 43, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(PanelBoard, javax.swing.GroupLayout.PREFERRED_SIZE, 490, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(toolbox, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE)
                    .addComponent(PanelTools, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, 264, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(922, 922, 922)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(177, 177, 177)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 493, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(83, 83, 83)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(PanelBoard, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 493, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(PanelTools, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(17, 17, 17)
                                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addComponent(toolbox, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)))))
                .addGap(186, 186, 186))
        );
    }// </editor-fold>//GEN-END:initComponents

private void guardarbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_guardarbtnActionPerformed
if(!"".equals(nombretxt.getText()) && nivelcmb.getSelectedIndex()!=-1 && !"".equals(descripciontxt.getText()) && !Soluciones.isEmpty()){
  
    int start = 2;
     ConectorBD conex = new ConectorBD();
     conex.conectar();
     if(blancasrb.isSelected())
         start=1;
    int Create_exercise = conex.Create_exercise(nivelcmb.getSelectedItem().toString(),nombretxt.getText(),descripciontxt.getText(),tablero.piece_tostring(),start);
    if(Create_exercise != 0){
      
       for(int i=0;i<Soluciones.size();i++){
        conex.Create_Solution(Create_exercise,Soluciones.get(i));
       }
     
    
    }
    
  //  System.out.println("ID"+Create_exercise);
    JOptionPane.showMessageDialog(null,"Ejercicio agregado correctamente","TEChess Training Suite",JOptionPane.INFORMATION_MESSAGE);
  
  PanelBoard.removeAll();
  toolbox.removeAll();
  load_editboard();
  dibujar_toolbox();
  limpiarbtn.setToolTipText("Limpiar el tablero");
  eliminarbtn.setVisible(true);
  toolbox.setVisible(true);
  Backbtn.setVisible(false);
  accept_solution.setVisible(false);
  Ordenarbtn.setVisible(true);
  nombretxt.setText("");
  nivelcmb.setSelectedIndex(0);
  descripciontxt.setText("");
  solutiontxt.setText("");
  tablero.limpiar();
  SelectedPiece = "";
    
    
    
}
else{
    if(Soluciones.isEmpty()){
    JOptionPane.showMessageDialog(null,"Debe agregar al menos una solución al ejercicio","Error",JOptionPane.ERROR_MESSAGE);
    }else
    JOptionPane.showMessageDialog(null,"Debe completar los campos requeridos para almacenar la jugada","Error",JOptionPane.ERROR_MESSAGE);

}
}//GEN-LAST:event_guardarbtnActionPerformed

private void limpiarbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_limpiarbtnActionPerformed
if(!SelectedPiece.equals("Solucion"))
    tablero.limpiar();
else{
    solutionBoard.piece =BoardStatus.clone();
    solutionBoard.update(this.getGraphics());
    Registry="";
    solutiontxt.setText("");
    solutionBoard.whiteTurn = turn;
    }
   
}//GEN-LAST:event_limpiarbtnActionPerformed

private void eliminarbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_eliminarbtnActionPerformed
            SelectedPiece = "Clean";
}//GEN-LAST:event_eliminarbtnActionPerformed

private void jToggleButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jToggleButton1ActionPerformed
             SelectedPiece=null;
             bg.clearSelection();
}//GEN-LAST:event_jToggleButton1ActionPerformed

private void AddSolutionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddSolutionActionPerformed
     Backbtn.setVisible(true); 
     
    if(Registry == ""){
       Soluciones = new  ArrayList<String>();
      
       if(Soluciones.isEmpty())
       BoardStatus = tablero.piece.clone();
       
       Ordenarbtn.setVisible(true);
       load_playboard();
       toolbox.removeAll();
       accept_solution.setVisible(true);
       Ordenarbtn.setVisible(false);
       toolbox.repaint();
       SelectedPiece="Solucion";
       limpiarbtn.setToolTipText("Volver a posicion inicial del ejercicio");
       eliminarbtn.setVisible(false);
       toolbox.setVisible(false);
       
      }
    else{
         int showConfirmDialog = JOptionPane.showConfirmDialog(null,"Desea almacenar la solucion creada y agregar otra?","Soluciones Alternativas",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
         if(showConfirmDialog == JOptionPane.YES_OPTION){
         Soluciones.add(Registry);
         Registry="";
        
         solutiontxt.setText("Solucion Agregada a la lista \n si desea almacenar el ejercicio\n presione guardar");
         JOptionPane.showMessageDialog(null,"Si desea agregar mas soluciones presione agregar solución","Agregada a la lista de soluciones",JOptionPane.INFORMATION_MESSAGE);
          solutionBoard.piece =BoardStatus.clone();
          solutionBoard.update(this.getGraphics());
          solutionBoard.whiteTurn = turn;
          solutionBoard.addMouseListener(this);
          solutionBoard.addMouseMotionListener(this);
          solutiontxt.setText("");
         }
    }
     
     
 
         
}//GEN-LAST:event_AddSolutionActionPerformed

private void accept_solutionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_accept_solutionActionPerformed
         if(!Registry.equals("")){
         Soluciones.add(Registry);
         Registry="";
         solutiontxt.setText("Solucion Agregada a la lista \n si desea almacenar el ejercicio\n presione guardar");
         JOptionPane.showMessageDialog(null,"Si desea agregar mas soluciones presione agregar solución","Agregada a la lista de soluciones",JOptionPane.INFORMATION_MESSAGE);
         }
         else{
         JOptionPane.showMessageDialog(null,"La solucion debe contener al menos un movimiento","Error",JOptionPane.ERROR_MESSAGE);
         }
}//GEN-LAST:event_accept_solutionActionPerformed

private void BackbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BackbtnActionPerformed
  int showConfirmDialog = JOptionPane.showConfirmDialog(null,"Desea volver al editor? \n Se eliminarán las soluciones creadas","Volver al Editor",JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
  if(showConfirmDialog == JOptionPane.YES_OPTION){
   PanelBoard.removeAll();
  load_editboard();
  dibujar_toolbox();
  limpiarbtn.setToolTipText("Limpiar el tablero");
  eliminarbtn.setVisible(true);
  toolbox.setVisible(true);
  Ordenarbtn.setVisible(true);
  Backbtn.setVisible(false);
  accept_solution.setVisible(false);
  solutiontxt.setText("");
  Registry="";
  Soluciones = new ArrayList<String>();  
  }
   
}//GEN-LAST:event_BackbtnActionPerformed

private void OrdenarbtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OrdenarbtnActionPerformed
   tablero.piece = tablero.startposition.clone();
   tablero.update(this.getGraphics());
}//GEN-LAST:event_OrdenarbtnActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddSolution;
    private javax.swing.JButton Backbtn;
    private javax.swing.JButton Ordenarbtn;
    private javax.swing.JPanel PanelBoard;
    private javax.swing.JPanel PanelBtn;
    private javax.swing.JPanel PanelTools;
    private javax.swing.JButton accept_solution;
    private javax.swing.JRadioButton blancasrb;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.ButtonGroup buttonGroup3;
    private javax.swing.ButtonGroup buttonGroup4;
    private javax.swing.ButtonGroup buttonGroup5;
    private javax.swing.ButtonGroup buttonGroup6;
    private javax.swing.JTextArea descripciontxt;
    private javax.swing.JToggleButton eliminarbtn;
    private javax.swing.JButton guardarbtn;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel23;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JToggleButton jToggleButton1;
    private javax.swing.JButton limpiarbtn;
    private javax.swing.JRadioButton negrasrb;
    private javax.swing.JComboBox nivelcmb;
    private javax.swing.JTextField nombretxt;
    private javax.swing.JTextArea solutiontxt;
    private javax.swing.JPanel toolbox;
    // End of variables declaration//GEN-END:variables
    private String SelectedPiece=""; 
    ButtonGroup bg = new ButtonGroup();
    private boolean turn;
    private  char BoardStatus[]; 
    private  char InitialBoard[]; 
    private int id_ex = 0;
    private List<String> Soluciones = new ArrayList<String>();
    private PlayBoard solutionBoard;
    private String Registry="";

}
